import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAdminStore = defineStore('admin', () => {
  const students = ref([])
  const majors = ref([])
  const classes = ref([])
  const dormitories = ref([])
  const registrations = ref([])
  const finances = ref([])
  const notices = ref([])
  const statistics = ref({})
  const loading = ref(false)

  // 获取统计数据
  const fetchStatistics = async () => {
    try {
      const mockStats = {
        totalStudents: 1200,
        registeredStudents: 980,
        paidStudents: 850,
        dormitoryOccupancy: 75,
        registrationRate: 81.7,
        paymentRate: 70.8,
        trends: {
          registration: [65, 72, 78, 81.7],
          payment: [45, 58, 65, 70.8]
        },
        majorDistribution: [
          { name: '计算机科学与技术', value: 320 },
          { name: '软件工程', value: 280 },
          { name: '数据科学与大数据技术', value: 250 },
          { name: '人工智能', value: 200 },
          { name: '网络工程', value: 150 }
        ]
      }
      
      statistics.value = mockStats
      return { success: true, data: mockStats }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 获取学生列表
  const fetchStudents = async (params = {}) => {
    loading.value = true
    try {
      const mockStudents = [
        {
          id: 1,
          studentId: '2024001',
          name: '张三',
          gender: '男',
          major: '计算机科学与技术',
          class: '计科2024-1班',
          phone: '13800138000',
          registrationStatus: 'completed',
          paymentStatus: 'paid',
          dormitory: '1号楼101-A'
        },
        {
          id: 2,
          studentId: '2024002',
          name: '李四',
          gender: '女',
          major: '软件工程',
          class: '软工2024-1班',
          phone: '13800138001',
          registrationStatus: 'in_progress',
          paymentStatus: 'unpaid',
          dormitory: '未分配'
        }
      ]
      
      students.value = mockStudents
      return { success: true, data: mockStudents, total: mockStudents.length }
    } catch (error) {
      return { success: false, message: error.message }
    } finally {
      loading.value = false
    }
  }

  // 获取专业列表
  const fetchMajors = async () => {
    try {
      const mockMajors = [
        {
          id: 1,
          name: '计算机科学与技术',
          college: '计算机学院',
          code: 'CS',
          description: '培养计算机科学与技术专业人才'
        },
        {
          id: 2,
          name: '软件工程',
          college: '计算机学院',
          code: 'SE',
          description: '培养软件工程专业人才'
        }
      ]
      
      majors.value = mockMajors
      return { success: true, data: mockMajors }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 获取班级列表
  const fetchClasses = async () => {
    try {
      const mockClasses = [
        {
          id: 1,
          name: '计科2024-1班',
          major: '计算机科学与技术',
          studentCount: 35,
          advisor: '王老师',
          advisorPhone: '13900139000'
        },
        {
          id: 2,
          name: '软工2024-1班',
          major: '软件工程',
          studentCount: 32,
          advisor: '李老师',
          advisorPhone: '13900139001'
        }
      ]
      
      classes.value = mockClasses
      return { success: true, data: mockClasses }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 获取宿舍信息
  const fetchDormitories = async () => {
    try {
      const mockDormitories = [
        {
          id: 1,
          building: '1号楼',
          floor: 1,
          room: '101',
          capacity: 4,
          occupied: 3,
          students: ['张三', '李四', '王五'],
          facilities: ['空调', '热水器', '独立卫浴']
        },
        {
          id: 2,
          building: '1号楼',
          floor: 1,
          room: '102',
          capacity: 4,
          occupied: 4,
          students: ['赵六', '钱七', '孙八', '周九'],
          facilities: ['空调', '热水器', '独立卫浴']
        }
      ]
      
      dormitories.value = mockDormitories
      return { success: true, data: mockDormitories }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 获取报到管理数据
  const fetchRegistrations = async () => {
    try {
      const mockRegistrations = [
        {
          id: 1,
          studentId: '2024001',
          studentName: '张三',
          registrationDate: '2024-09-01',
          status: 'completed',
          steps: {
            infoConfirm: true,
            payment: true,
            dormitory: true,
            finalCheck: true
          }
        }
      ]
      
      registrations.value = mockRegistrations
      return { success: true, data: mockRegistrations }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 获取财务数据
  const fetchFinances = async () => {
    try {
      const mockFinances = [
        {
          id: 1,
          studentId: '2024001',
          studentName: '张三',
          items: [
            { name: '学费', amount: 5000, status: 'paid', paidDate: '2024-08-15' },
            { name: '住宿费', amount: 1200, status: 'paid', paidDate: '2024-08-15' }
          ],
          totalAmount: 6200,
          paidAmount: 6200,
          unpaidAmount: 0
        }
      ]
      
      finances.value = mockFinances
      return { success: true, data: mockFinances }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 获取通知公告
  const fetchNotices = async () => {
    try {
      const mockNotices = [
        {
          id: 1,
          title: '2024年新生报到须知',
          content: '请各位新生按时报到，携带相关证件...',
          publishDate: '2024-08-01',
          status: 'published',
          author: '管理员'
        }
      ]
      
      notices.value = mockNotices
      return { success: true, data: mockNotices }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 添加/更新学生
  const saveStudent = async (studentData) => {
    loading.value = true
    try {
      if (studentData.id) {
        // 更新
        const index = students.value.findIndex(s => s.id === studentData.id)
        if (index !== -1) {
          students.value[index] = { ...students.value[index], ...studentData }
        }
      } else {
        // 新增
        const newStudent = {
          ...studentData,
          id: Date.now()
        }
        students.value.push(newStudent)
      }
      return { success: true, message: '保存成功' }
    } catch (error) {
      return { success: false, message: error.message }
    } finally {
      loading.value = false
    }
  }

  // 删除学生
  const deleteStudent = async (studentId) => {
    try {
      const index = students.value.findIndex(s => s.id === studentId)
      if (index !== -1) {
        students.value.splice(index, 1)
      }
      return { success: true, message: '删除成功' }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 发布通知
  const publishNotice = async (noticeData) => {
    loading.value = true
    try {
      const newNotice = {
        ...noticeData,
        id: Date.now(),
        publishDate: new Date().toISOString().split('T')[0],
        status: 'published',
        author: '管理员'
      }
      notices.value.unshift(newNotice)
      return { success: true, message: '发布成功' }
    } catch (error) {
      return { success: false, message: error.message }
    } finally {
      loading.value = false
    }
  }

  return {
    students,
    majors,
    classes,
    dormitories,
    registrations,
    finances,
    notices,
    statistics,
    loading,
    fetchStatistics,
    fetchStudents,
    fetchMajors,
    fetchClasses,
    fetchDormitories,
    fetchRegistrations,
    fetchFinances,
    fetchNotices,
    saveStudent,
    deleteStudent,
    publishNotice
  }
})